apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../dialog"], function(V, $api, $, app, service, dialog) {
      app.ready();
        var vm = {
            el: '.container',
            data: {
                classifyList: [], //--分类之后的商品列表
                searchName: ''
            },
            mounted: function() {
                //--解决ios输入法挡住输入框的问题
                $(document).on("focus", "#aui-input", function() {
                    console.log($('body').scrollTop());
                    var top = $('body').scrollTop();
                    setTimeout(function() {
                        console.log(document.body.offsetHeight);
                        window.scrollTo(0, top + 100);
                    }, 50);
                });
            },
            methods: {
                //--搜索事件
                doSearch: function(et) {
                    this.page = 1;
                    this.searchName = $("#search-input").val();
                    //获取分类后的商品列表
                    this.getClassifyList();
                },
                //获取下拉分类列表
                getClassifyList: function() {
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var searchName = this.searchName;
                    var that = this;

                    app.ajax(service['getCategory'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    shop_code: shopId,
                                    keyword: searchName
                                }
                            }
                        },
                        function(res) {
                            // var shopCode = res.data.shop_code; // 商店id
                            if (res.code) {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                that.classifyList = res.data;
                            }
                        });
                },
                //--新建分类
                newClassify: function() {
                    var that = this;
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id

                    dialog.prompt({
                        title: "新建分类",
                        text: "输入分类名称",
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        // alert(JSON.stringify(ret));
                        if (ret.buttonIndex == 2) { //  确认
                            var name = $api.trim(ret.text);

                            if (name == '' || name == null) {
                                api.toast({
                                    msg: "请输入正确的分类名称...",
                                    duration: 2000,
                                    location: 'middle'
                                });
                                name.value = '';
                                return false;
                            } else if (name.length > 10) {
                                api.toast({
                                    msg: "名称字数超过上限...",
                                    duration: 2000,
                                    location: 'middle'
                                });
                                return false;
                            }
                            //--判断是否存在特殊字符
                            if (app.hasSpecialChar(name, '请正确输入分类名称...', '')) {
                                return
                            }

                            app.ajax(service['addCategory'], {
                                    data: {
                                        values: {
                                            shop_code: shopId,
                                            unionid: unionid,
                                            name: name
                                        }
                                    }
                                },
                                function(res) {
                                    //返回category_id
                                    if (res.code) {
                                        api.toast({
                                            msg: res.message,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        that.category_id = res.data;
                                        console.log(this.category_id);
                                        alert("新建分类成功！");

                                        that.getClassifyList();
                                    }
                                });
                        }
                    })
                },
                showEdit: function() {
                    $('.edit_group').css("display", "block");
                    $('.shop_footer').css("display", "none");
                    $('.shop_footer_finish').css("display", "block");
                },
                finishEdit: function() {
                    $('.edit_group').css("display", "none");
                    $('.shop_footer_finish').css("display", "none");
                    $('.shop_footer').css("display", "flex");
                },
                //--删除分类
                activeCat: function(category_id) {
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    dialog.alert({
                        title: "分类回收提示",
                        msg: '请确认是否删除该分类',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            app.ajax(service['categoryActive'], {
                                    data: {
                                        values: {
                                            shop_code: shopId,
                                            unionid: unionid,
                                            category_id: category_id
                                        }
                                    }
                                },
                                function(res) {
                                    //返回category_id
                                    if (res.code) {
                                        api.toast({
                                            msg: res.message,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        api.toast({
                                            msg: '已删除分类',
                                            duration: 2000,
                                            location: 'middle'
                                        });

                                        that.getClassifyList();
                                    }
                                });
                        }
                    })
                },
                //--编辑分类名称
                editCat: function(category_id, classifyName) {
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    dialog.prompt({
                        title: "编辑该分类名称",
                        text: "输入分类名称",
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        // alert(JSON.stringify(ret));
                        if (ret.buttonIndex == 2) { //  确认
                            var name = ret.text;
                            if (name == '' || name == null) {
                                alert("请输入正确的分类名称...")
                                return false;
                            } else if (name == classifyName) {
                                alert("与原名称相同...")
                                return false;
                            } else if (name.length > 10) {
                                alert("名称字数超过上限...")
                                return false;
                            }

                            app.ajax(service['categoryEdit'], {
                                    data: {
                                        values: {
                                            shop_code: shopId,
                                            unionid: unionid,
                                            category_id: category_id,
                                            name: name
                                        }
                                    }
                                },
                                function(res) {
                                    //返回category_id
                                    if (res.code) {
                                        api.toast({
                                            msg: res.message,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        api.toast({
                                            msg: "分类名称已更新！",
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                        that.getClassifyList();
                                    }
                                });
                        }
                    })
                },
            },
            created: function() {
                //获取店铺商品分类列表
                this.getClassifyList();

                api.addEventListener({
                    name: 'keyboardshow'
                }, function(ret, err) {
                    $('.header').css({
                        'position': 'static'
                    });
                    $(".container.top-container").css("cssText", "padding-top: 0rem !important;");
                    $('.aui-searchbar').css({
                        'position': 'static'
                    });
                    $('.goods_search_container').css({
                        'margin-top': 0
                    });
                });

                api.addEventListener({
                    name: 'keyboardhide'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'fixed'
                    });
                    $(".container.top-container").css("cssText", "padding-top: 3rem !important;");
                    $('.aui-searchbar').css({
                        'top': '3rem',
                        'position': 'fixed'
                    });
                    $('.goods_search_container').css({
                        'margin-top': '2.2rem'
                    });
                });

            }
        };
        // var shopVue = new Vue(vm);
        var even = function() {
            // dialog = new auiDialog({});
            // pi.addEventListener({
            //     name: 'scrolltobottom',
            // 		extra:{
            //         threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
            //     }
            // , function(ret, err){
            // 		vue.scrolltobottom()
            // );

        };
        var newVue = new Vue(vm);
        even();

    })
}
